//
//  ShipuViewController.m
//  ThePrivateKitchen
//
//  Created by lirui on 16/1/20.
//  Copyright © 2016年 www.lanou3g.com蓝鸥科技. All rights reserved.
//

#import "ShipuViewController.h"
#import "SearchViewController.h"

static NSInteger i = 0;
@interface ShipuViewController ()

@end
@interface ShipuViewController ()<UITableViewDataSource,UITableViewDelegate,LunBoTuDelegate,UICollectionViewDataSource,UICollectionViewDelegate>
@property(nonatomic, retain)NSMutableArray *dataArr;  //
@property(nonatomic, retain)UITableView *tableview;
@property(nonatomic,retain)UIView *aView;   //头部大得自定义视图
@property(nonatomic,retain)ShipuView  *shipuView;    //热门推荐自定义视图
@property(nonatomic,retain)ShipuView_zhuanti *zView;  //专题的自定义视图
@property(nonatomic, retain)Shipu_hot *hot;  //热门菜的自定义视图
@property(nonatomic ,retain)NSMutableArray  *nArr;   //新品推荐的数组
@property(nonatomic,retain)UIImageView *lunboView;  //轮播图的图片数组
@property(nonatomic ,retain)NSMutableArray *lunArr; //轮播图数据的数组
@property(nonatomic,retain)UICollectionView *collectionview;
@property(nonatomic, retain)NSMutableArray *collArr; //导航数据的数组

@property(nonatomic, retain)NSMutableArray *idArr;//存放id的数组
@property(nonatomic, retain)UISearchBar *searchBar;
@property(nonatomic ,assign)BOOL isRefresh;
@end

@implementation ShipuViewController

- (void)dealloc
{  [_dataArr release];
    [_tableview release];
    [_aView release];
    [_shipuView release];
    [_zView release];
    [_hot release];
    [_nArr release];
    [_lunArr release];
    [_collectionview release];
    [_collArr release];
    [_lunboView release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.translucent= YES;
    [self creatTableView];
    [self creatSearch];
    [self AFNPOST];
    [self post];
    [self post1];
    [self post2];
    [self post3];
    //创建头部大的自定义视图
    self.aView = [[UIView alloc] initWithFrame:CGRectMake(0, HEIGHT *200, WIDTH*375, HEIGHT*1240)];
    self.aView.backgroundColor =[ UIColor lightGrayColor];
    self.tableview .tableHeaderView = self.aView;
    
    UILabel *label = [[ UILabel alloc] initWithFrame:CGRectMake(0,HEIGHT*1220, WIDTH*280,HEIGHT* 42)];
    label.backgroundColor = [ UIColor whiteColor];
    label.text = @"  推荐达人";
    UIButton *btn = [ UIButton buttonWithType:UIButtonTypeSystem];
    btn.frame = CGRectMake(WIDTH*280, HEIGHT*1220, WIDTH*95, HEIGHT*42);
    [btn setTitle:@"更多  >" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    btn.backgroundColor = [ UIColor  whiteColor];
    [self.aView addSubview:btn];
    btn.tag = 404;
    [self.aView addSubview:label];
    //轮播图
    self.lunboView =[[ UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH *375, HEIGHT * 150)];
    _lunboView.backgroundColor = [ UIColor whiteColor];
    [self.aView addSubview:_lunboView];
    
    //热门推荐的视图
    [self creatShipuView];
    //热门专题的视图
    [self creatShipuView2];
    //热门菜的视图
    [self creatShipuView3];
    //创建达人的滚动视图
    [self creatcollectionview];
   
    [self loadMBProgress];
    
    [_lunboView release];
    [label release];
    [_aView release];
    
  
    
    
    
}
//AFN请求数据 热门推荐数据请求
-(void)AFNPOST
{
    NSString *str = @"http://api.izhangchu.com/?methodName=HomeIndex&token=0&user_id=0&version=4.1";
    [LRAFNetwork postUrlString:str body:nil response:JSON bodyStyle:RequestJSON requestHeadFile:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        NSArray *arr  = responseObject[@"data"][@"data"];
        self.dataArr = [ NSMutableArray array];
        self.idArr = [ NSMutableArray array];
        NSArray *temparr = arr[0][@"data"];
        self.shipuView.label1.text =  temparr[0][@"title"];
        self.shipuView.label2.text = temparr[0][@"description"];
        [self.shipuView.imgView1 sd_setImageWithURL:[NSURL URLWithString:temparr[0][@"image"]]];

        self.shipuView.label3.text =  temparr[1][@"title"];
        self.shipuView.label4.text = temparr[1][@"description"];
        [self.shipuView.imgView2 sd_setImageWithURL:[NSURL URLWithString:temparr[1][@"image"]]];
        
        [self.idArr addObject:temparr[0][@"video1"]];
        [self.idArr addObject:temparr[1][@"video1"]];
        [_tableview reloadData];
        [self.hud removeFromSuperview];

            }
     failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                           NSLog(@"失败");
                       }];
    
    
    
}
//专题数据请求
- (void)post
{
    NSString *str = @"http://api.izhangchu.com/?methodName=HomeIndex&token=0&user_id=0&version=4.1";
    [LRAFNetwork postUrlString:str body:nil response:JSON bodyStyle:RequestJSON requestHeadFile:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSArray *arr  = responseObject[@"data"][@"data"];
        self.dataArr = [ NSMutableArray array];
        
        NSArray *temparr = arr[1][@"data"];
        self.zView.label1.text =  temparr[0][@"title"];
        self.zView.label2.text = temparr[0][@"description"];
        [self.zView.imgView1 sd_setImageWithURL:[NSURL URLWithString:temparr[0][@"image"]]];
        
        self.zView.label3.text =  temparr[1][@"title"];
        self.zView.label4.text = temparr[1][@"description"];
        [self.zView.imgView2 sd_setImageWithURL:[NSURL URLWithString:temparr[1][@"image"]]];
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
    }];
}
//热门菜数据请求
-(void)post1
{
    NSString *str = @"http://api.izhangchu.com/?methodName=HomeIndex&token=0&user_id=0&version=4.1";
    [LRAFNetwork postUrlString:str body:nil response:JSON bodyStyle:RequestJSON requestHeadFile:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSArray *arr  = responseObject[@"data"][@"data"];
        self.dataArr = [ NSMutableArray array];
        
        NSArray *temparr = arr[2][@"data"];
        self.hot.label1_1.text =  temparr[0][@"title"];
        self.hot.label1_2.text = temparr[0][@"description"];
        [self.hot.imgView1 sd_setImageWithURL:[NSURL URLWithString:temparr[0][@"image"]]];
        
        
        self.hot.label2_1.text =  temparr[1][@"title"];
        self.hot.label2_2.text = temparr[1][@"description"];
        [self.hot.imgView2 sd_setImageWithURL:[NSURL URLWithString:temparr[1][@"image"]]];
        
        self.hot.label3_1.text =  temparr[2][@"title"];
        self.hot.label3_2.text = temparr[2][@"description"];
        [self.hot.imgView3 sd_setImageWithURL:[NSURL URLWithString:temparr[2][@"image"]]];
    }
     failure:^(NSURLSessionDataTask *task, NSError *error) {
                       }];
    
}
//导航栏数据请求
-(void)post2
{
    NSString *str = @"http://api.izhangchu.com/?methodName=HomeIndex&token=0&user_id=0&version=4.1";
    [LRAFNetwork postUrlString:str body:nil response:JSON bodyStyle:RequestJSON requestHeadFile:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSArray *cArr = responseObject[@"data"][@"navigate"];
        self.collArr = [ShipuModel modelHanderWithArray:cArr];
        
        [_collectionview reloadData];
        [_tableview reloadData];
         [self.tableview.mj_header endRefreshing];
    }
    failure:^(NSURLSessionDataTask *task, NSError *error) {
                       }];
    
}
//轮播图数据请求
-(void)post3
{
    NSString *str = @"http://api.izhangchu.com/?methodName=HomeIndex&token=0&user_id=0&version=4.1";
    [LRAFNetwork postUrlString:str body:nil response:JSON bodyStyle:RequestJSON requestHeadFile:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSArray *arr  = responseObject[@"data"][@"banner"];
        
        self.lunArr = [ NSMutableArray array];
        self.lunArr = [ ShipuModel modelHanderWithArray:arr];
        NSMutableArray *temp =[ NSMutableArray array];
        for (NSInteger i = 0; i < _lunArr.count; i++) {
            ShipuModel *m =  [self.lunArr objectAtIndex:i];
            [temp addObject:m.banner_picture];
        }
        LunBoTu *lun = [[ LunBoTu alloc] init];
        lun.delegate = self;
        lun.time = 2;
        [lun imageWithUrlArray:temp frame:CGRectMake(0, 0, WIDTH *375,HEIGHT * 150)];
        [self.aView addSubview:[lun collectionView]];
        [self.aView addSubview:[lun pageControl]];
        
        
    }
failure:^(NSURLSessionDataTask *task, NSError *error) {
                       }];
    
}
//创建自定义的热门推荐view
-(void)creatShipuView
{
    self.shipuView = [[ShipuView alloc] initWithFrame:CGRectMake(0, HEIGHT * 300, WIDTH * 375,HEIGHT * 355)];
    self.shipuView .backgroundColor = [UIColor whiteColor];
    _shipuView.title.text = @"热门推荐";
    
    [self.aView addSubview:self.shipuView];
    /**block传值*/
    //第一个图片点击
    BLOCK pushimg1 = ^()
    {
       NewViewController *video = [[ NewViewController alloc] init];
        video.string = self.idArr[0];
        [self.navigationController pushViewController:video animated:YES];
        
    };
    _shipuView.blockimg1 = pushimg1;
    //第二个图片点击
    /**block传值*/
    BLOCK pushimg2 = ^()
    {
        NewViewController *video = [[ NewViewController alloc] init];
        video.string = self.idArr[1];
        [self.navigationController pushViewController:video animated:YES];
    };
    _shipuView.blockimg2 = pushimg2;
    UIButton *hotBtn = [ UIButton buttonWithType:UIButtonTypeSystem];
    hotBtn.frame = CGRectMake(WIDTH *280,HEIGHT * 10,WIDTH* 95,HEIGHT * 30);
    [hotBtn setTitle:@"更多  >" forState:UIControlStateNormal];
    [hotBtn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    [_shipuView addSubview:hotBtn];
    hotBtn.tag = 401;
    
    [_shipuView release];
}
//创建自定义的专题view
-(void)creatShipuView2
{
    self.zView =[[ ShipuView_zhuanti alloc] initWithFrame:CGRectMake(0,HEIGHT * 655,WIDTH * 375,HEIGHT * 365)];
    _zView.backgroundColor = [ UIColor whiteColor];
    _zView.title.text = @"专题";
    [self.aView addSubview:self.zView];
    
   
    UIButton *zhuantiBtn = [ UIButton buttonWithType:UIButtonTypeSystem];
    zhuantiBtn.frame = CGRectMake(WIDTH * 280,HEIGHT * 10,WIDTH * 95,HEIGHT * 30);
    [zhuantiBtn setTitle:@"更多  >" forState:UIControlStateNormal];
    [zhuantiBtn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    [_zView addSubview:zhuantiBtn];
    zhuantiBtn.tag = 402;
    
    [_zView release];
}
//创建自定义的排行榜view
-(void)creatShipuView3
{
    self.hot =[[ Shipu_hot alloc] initWithFrame:CGRectMake(0,HEIGHT * 1020,WIDTH * 375,HEIGHT * 200)];
    _hot.backgroundColor = [ UIColor whiteColor];
    _hot.title .text = @"本周热门菜";
    [self.aView addSubview:self.hot];
    UIButton *hotDishBtn = [ UIButton buttonWithType:UIButtonTypeSystem];
    hotDishBtn.frame = CGRectMake(WIDTH * 280,HEIGHT * 10,WIDTH * 95,HEIGHT * 30);
    [hotDishBtn setTitle:@"更多  >" forState:UIControlStateNormal];
    [hotDishBtn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    [_hot addSubview:hotDishBtn];
    hotDishBtn.tag = 403;
    
    [_hot release];
}
#pragma mark -推荐达人的tableview
-(void)creatTableView
{ self.tableview =[[ UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH *375, HEIGHT *667) style:UITableViewStylePlain];
    _tableview.backgroundColor =[ UIColor whiteColor];
    _tableview.dataSource = self;
    _tableview.delegate = self;
    _tableview.separatorStyle  =UITableViewCellSeparatorStyleNone;
    [_tableview registerClass:[ShipuTableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:self.tableview];
    [_tableview release];
    
    MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingBlock:^{
        //赋初值
        i = 1;
        self.isRefresh = YES;
        
        [self AFNPOST];
        [self post];
        [self post1];
        [self post2];
        [self post3];
        
        
    }];
    self.tableview.mj_header = header;
    

    
    
}
//cell数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
//cell设置
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   ShipuTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];

    return cell;
}
//行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 200*HEIGHT;
}

#pragma mark -分类collectionview
-(void) creatcollectionview
{
    UICollectionViewFlowLayout  *layout = [[ UICollectionViewFlowLayout alloc] init];
    //布局设置
    //滚动方向
    layout.scrollDirection =  UICollectionViewScrollDirectionHorizontal;
    //cell大小
    layout.itemSize = CGSizeMake((WIDTH * 375 -40* WIDTH )/4 ,HEIGHT * 80/2);
    //行列间距
    //行
    layout.minimumLineSpacing = 10*WIDTH;
    //列
    layout.minimumInteritemSpacing = 10*HEIGHT;
    //边界
    layout.sectionInset = UIEdgeInsetsMake( WIDTH * 10,HEIGHT * 30 ,WIDTH * 30, HEIGHT * 20);
    self.collectionview =[[ UICollectionView alloc] initWithFrame:CGRectMake(0,150*HEIGHT, WIDTH*375, HEIGHT*150)   collectionViewLayout:layout];
    self.collectionview .delegate = self;
    self.collectionview.dataSource = self;
    self.collectionview.backgroundColor = [UIColor whiteColor];
    _collectionview.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
    [self.aView addSubview:self.collectionview];
    //[_collectionview setContentOffset:CGPointMake(375 * 2, 0)];
    _collectionview.pagingEnabled = YES;
    [_collectionview registerClass:[ShipuCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    
}
//item个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 16;
}
//cell设置
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    //获取cell
    ShipuCollectionViewCell *cell = [ collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    
    ShipuModel *m =  _collArr[indexPath.item];
    
    cell.title .text =m.text ;
    [cell.imgview sd_setImageWithURL:[NSURL URLWithString:m.image]];
    
    return cell;
    
    
}


//collecitonview 导航 点击方法

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    //判断跳转不同的界面
    if (indexPath.item == 0 || indexPath.item == 1 ) {
        NavigateViewController *ni_VC = [[NavigateViewController alloc] init];
        ni_VC.s_Model = _collArr [indexPath.item];
    
    [self.navigationController pushViewController:ni_VC animated:YES];
    [ni_VC release];
    }
   else if (indexPath.item == 2 || indexPath.item == 3) {
        MaterialClassViewController *materialVC =[[ MaterialClassViewController alloc] init];
        materialVC.shi_model = _collArr[ indexPath.item];
        
        [self.navigationController pushViewController:materialVC animated:YES];
        [materialVC release];
    }
        else
        {
            SancanViewController *sancan = [[ SancanViewController alloc] init];
            sancan.s_model = _collArr[indexPath.row];
            [self.navigationController pushViewController:sancan animated:YES];
            
        }
    
}
//轮播图点击方法
-(void)didSelectCollectionView:(UICollectionView *)collectionView atItemImdexPath:(NSIndexPath *)indexPath
{
    LunbotuViewController *lunbo = [[ LunbotuViewController alloc] init];
    lunbo.webModel = _lunArr[indexPath.row];
    [self.navigationController pushViewController:lunbo animated:YES];
}
//button 点击方法
-(void)btnAction:(UIButton *)btn
{   //热门推荐
    if (btn.tag == 401 ) {
        NSLog(@"1111");
        HotMoreViewController *more = [[ HotMoreViewController alloc] init];
        more.tag = btn.tag;
        [self.navigationController pushViewController:more animated:YES];

    }
    //专题
   else if (btn.tag == 402) {
     
        HotMoreViewController *more_zhuanti = [[HotMoreViewController alloc] init];
        more_zhuanti.tag = btn.tag;
        [self.navigationController pushViewController:more_zhuanti animated:YES];
        
    }
/**热门菜*/
    else if(btn.tag == 403)
    {
        HotMoreViewController *more_food = [[HotMoreViewController alloc] init];
        more_food.tag = btn.tag;
        [self.navigationController pushViewController:more_food animated:YES];
    }
    //推荐达人
   else if (btn.tag == 404) {
     SuperMamViewController *sVC =[[ SuperMamViewController alloc] init];
       [self.navigationController pushViewController:sVC animated:YES];
       
    }
}

-(void)creatSearch
{
    /*
     *  创建titleView
     */
    // 初始化
    UIButton *titleView = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 280*WIDTH , 30* WIDTH )];
    // 点击事件
    [titleView addTarget:self action:@selector(clickOnTitleView) forControlEvents:UIControlEventTouchUpInside];
    // 设置为titleView
    self.navigationItem.titleView = titleView;
   
    // 初始化
    self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(5* WIDTH , 5* WIDTH , 260* WIDTH , 20* WIDTH )];
    self.searchBar.barTintColor = [UIColor orangeColor];
    // searchBar类型
    [self.searchBar setBarStyle:UIBarStyleDefault];
    // 提示文本
    self.searchBar.placeholder = @"输入菜名或食材搜索";
    // 键盘类型
    [self.searchBar setKeyboardType:UIKeyboardTypeDefault];
    // 视图剪切
    self.searchBar.layer.masksToBounds = YES;
    // 设置圆角
    self.searchBar.layer.cornerRadius = 10* WIDTH ;
    // 用户交互关掉
    self.searchBar.userInteractionEnabled = NO;
    // 将searchBar添加到titleView上
    [titleView addSubview:self.searchBar];
    [_searchBar release];

}
- (void)clickOnTitleView
{
    SearchViewController *search =[[ SearchViewController alloc] init];
    [self.navigationController pushViewController:search animated:YES];
    
   }
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the New view controller using [segue destinationViewController].
    // Pass the selected object to the New view controller.
}
*/

@end
